package de.riceman.simpleGo.main;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class PositionTable implements Cloneable {

	private List<BitMatrix> positions;

	public PositionTable () {
		positions = new ArrayList<BitMatrix>();
	}
	
	public boolean containsPosition(BitMatrix dark, BitMatrix light, boolean player) {
	
		return positions.contains(this.getPositionKey(dark, light, player));
	}
	
	public boolean contains(BitMatrix dark, BitMatrix light, boolean player) {
		if (positions.contains(getPositionKey(dark, light, player))) {
			return true;
		} else {
			return false;
		}
	}
	
	public void addPosition(BitMatrix dark, BitMatrix light, boolean player) {

		positions.add(this.getPositionKey(dark, light, player));
	}
	
	public BitMatrix getPositionKey(BitMatrix dark, BitMatrix light, boolean player) {
		BitMatrix key = new BitMatrix(1, (dark.getColumns() * 2) + 1);
		//BitMatrix key = BitMatrixFactory.getInstance().getNewBitMatrix(1, (dark.getColumns() * 2) + 1);
		
		for (int i = 0; i < dark.getColumns(); i++) {
			//set dark player's stones
			key.set(0, i, dark.get(i));
			//set light player's stones
			key.set(0, i+dark.getColumns(), light.get(i));
		}
		//set player's turn at the end
		key.set(0, dark.getColumns()*2 , player);
		
		return key;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		
		Iterator<BitMatrix> it = positions.iterator();
		StringBuffer out = new StringBuffer();
		
		while (it.hasNext()) {
			out.append(it.next().toString() + "\n");
		}
		
		return out.toString();
		
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	protected Object clone() {
		Iterator<BitMatrix> it;
		PositionTable newPositionTable = new PositionTable();
		
		it = positions.iterator();
		
		while (it.hasNext()) {
			newPositionTable.positions.add((BitMatrix) it.next().clone());
		}
		
		return newPositionTable;
	}
	
	/**
	 * @return the positions
	 */
	public List<BitMatrix> getPositions() {
		return positions;
	}

	/**
	 * @param positions the positions to set
	 */
	public void setPositions(List<BitMatrix> positions) {
		this.positions = positions;
	}
}